package com.brightfuture.chrms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brightfuture.chrms.entity.PositionEntity;
import com.brightfuture.chrms.mapper.PositionMapper;
import com.brightfuture.chrms.service.PositionService;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.brightfuture.chrms.entity.PositionStandardEntity;
import com.brightfuture.chrms.mapper.PositionStandardMapper;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;

import java.util.Objects;

/**
 * 岗位信息
 *
 * @author victor
 * @date 2024-02-04 22:31:50
 */
@Service
@RequiredArgsConstructor
public class PositionServiceImpl extends ServiceImpl<PositionMapper, PositionEntity> implements PositionService {
    private final PositionStandardMapper positionStandardMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDeep(PositionEntity position) {
        baseMapper.insert(position);
        for (PositionStandardEntity positionStandard : position.getPositionStandardList()) {
            positionStandard.setPositionId(position.getPositionId());
            positionStandardMapper.insert(positionStandard);
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDeep(PositionEntity position) {
        baseMapper.updateById(position);
        for (PositionStandardEntity positionStandard : position.getPositionStandardList()) {
            if (Objects.isNull(positionStandard.getPositionId())) {
                positionStandard.setPositionId(position.getPositionId());
                positionStandardMapper.insert(positionStandard);
            } else {
                positionStandardMapper.updateById(positionStandard);
            }
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDeep(Long[] ids) {
        baseMapper.deleteBatchIds(CollUtil.toList(ids));
        positionStandardMapper.delete(Wrappers.<PositionStandardEntity>lambdaQuery().in(PositionStandardEntity::getPositionId, ids));
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeChild(Long[] ids) {
        positionStandardMapper.deleteBatchIds(CollUtil.toList(ids));
        return Boolean.TRUE;
    }
}